<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\9 0009
 * Time: 13:51
 */
namespace app\index\controller;

use think\Controller;
use Think\Exception;
use think\facade\Session;

class Login extends Controller{

    public function initialize(){
        $this->loginModel = model('common/login');
        $this->userModel = model('common/user');
        $this->userGroupModel = model('common/userGroup');
        $this->groupModel = model('common/group');
    }

    public function login(){
        $user_id = Session::get('user_id');
        if($user_id){
            $this->redirect('/');
        }
        if(request()->isPost()){
            $mail = $this->request->post('mail','');
            $password = $this->request->post('password','');

            if(empty($mail) || empty($password)){
                return json(['code'=>-1,'message'=>'请先填写邮箱和密码']);
            }

            $ret = $this->userModel->findUserInfoByMail($mail);

            if(!$ret){
                return json(['code'=>-2,'message'=>'用户不存在，请先注册']);
            }

            $user = $this->userModel->findUser($mail,$password);

            if(empty($user)){
                return json(['code'=>-3,'message'=>'密码错误']);
            }else{
                Session::set('user_id',$user->id);
                Session::set('session_timeout',time());
                Session::set('user_name',$user->username);
                model('common/user')->setStatus($user_id,\app\common\model\User::ON_LINE);
                return json(['code'=>1,'message'=>'登录成功']);
            }
        }
        return $this->fetch();
    }

    public function logout(){
        Session::delete('user_id');
        Session::delete('session_timeout');
        Session::delete('user_name');
        return json(['code'=>1,'message'=>'登出成功']);
    }

    public function signin(){
        $post = $this->request->post();

        $re = $this->loginModel->checkCode($post['code'],$post['email']);

        if($re['code'] != 1 ){
            return $re;
        }else{
            $post['avatar'] = '/uploads/default/default.jpeg';
            $post['password'] = md5($post['password']);
            $post['status'] = \app\common\model\User::OUT_LINE;
            $re = $this->userModel->_save($post);
            if($re){
                $this->userGroupModel->_save(['user_id'=>$re,'group_id'=>1]);
                $this->groupModel->where('id', 1)->setInc('group_count');
                return json(['code'=>1,'message'=>'注册成功']);
            }else{
                return json(['code'=>-4,'message'=>'注册失败']);
            }
        }
    }

    public function reset(){
        $post = $this->request->post();

        if($post['password'] != $post['repassword']){
            return ['code'=>-1,'message'=>'两次密码不一致'];
        }

        $userinfo = $this->userModel->findUserInfoByMail($post['email']);

        if(!$userinfo){
            return ['code'=>-2,'message'=>'邮箱不存在,请先注册账户'];
        }

        if($userinfo->password == md5($post['password'])){
            return ['code'=>-3,'message'=>'新密码不得与原密码一致'];
        }

        $re = $this->loginModel->checkCode($post['code'],$post['email']);

        if($re['code'] != 1 ){
            return $re;
        }else{
            $userinfo->password = md5($post['password']);
            $ret = $userinfo->save();
            if($ret){
                $this->loginModel->sendResetSuccessMail($post['email'],$userinfo->username);
                return ['code'=>1,'message'=>'重置密码成功'];
            }else{
                return ['code'=>-4,'message'=>'重置密码失败'];
            }
        }
    }

    public function sendEmail(){
        $to_email = $this->request->post('email','');
        $type = $this->request->post('type',1);
        if(!$to_email){
            return json(['code'=>-1,'message'=>'请先输入邮箱']);
        }

        $ret = $this->userModel->findUserInfoByMail($to_email);

        if($type == 1 && $ret){
            return json(['code'=>-2,'message'=>'邮箱已存在']);
        }elseif($type == 2 && !$ret){
            return json(['code'=>-2,'message'=>'邮箱不存在,请先注册账户']);
        }

        $code = $this->loginModel->makeCode($to_email);

        $re = $this->loginModel->sendMail($to_email,$code,$type);

        if($re){
            return json(['code'=>1,'message'=>'邮件发送成功']);
        }else{
            return json(['code'=>-3,'message'=>'邮件发送失败']);
        }
    }
}